<template>
	<JkScrollList 
	:isTop="isTop"
	:isSearch="isSearch"
	:isBanner="isBanner"
	:isClick="isClick"
	:navData="navData"
	:listData="listData"
	:pageData="pageData"
	:currentTab="currentTab"
	:max="max"
	@changeTab="changeTab" 
	@loadMore="loadMore"
	@isRefresh="isRefresh"
	ref="scroll"
	></JkScrollList>
</template>

<script>
	const util = require('../../../utils/jk-throttle.js');
	import JkScrollList from '@/components/jk-scrollList/jkScrollList';
	
	export default {
		data() {
			return {
				isSearch:1, //是否启用搜索框
				isBanner:1, //是否启用轮播图
				isClick:0,//是否启用点击反馈组件(点击反馈组件目前还有些bug)
				isTop:1, //是否展示回到顶部按钮
				currentTab:0, //sweiper所在页
				max:[0,0,0,0,0,0,0,0,0], //数据请求完毕
				pageData:[1,1,1,1,1,1,1,1,1],  //每个swiper的第几页
				//导航栏数据
				navData:['选择一','asdooijhoahsdhoh','选择三','选择四','asdasdasdasd','选择六','选择七asda','选择八','选择九'], 
				 //列表数据
				listData:[[1, 2, 3, 4, 5, 6],['a', 'b', 'c', 'd', 'e', 'f'],[],['2233','4234','13144','324244'],[1, 2, 3, 4, 5, 6],['a', 'b', 'c', 'd', 'e', 'f'],['7'],['8'],['9号']],
			}
		},
		components: {
			JkScrollList
		},
		methods: {
			//上拉刷新事件
			isRefresh(){
				console.log('刷新事件执行');
			},
			//切换tab事件
			changeTab(index){
				console.log(index);
				this.currentTab = index
			},
			//其他请求事件 当然刷新和其他请求可以写一起 多一层判断。
			isRequest(pageData) {
				return new Promise((resolve, reject) => {
					this.pageData[this.currentTab]++
					var that = this
					if(this.pageData[this.currentTab]<5){//模拟数据加载完毕
						setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: `请求第${that.currentTab + 1 }个导航栏的第${that.pageData[that.currentTab]}页数据成功`
						})
						let newData = ['新数据1','新数据2','新数据3']
						resolve(newData)
					}, 1000)
					}else{
						uni.showToast({
								icon: 'none',
								title: `加载完成`
							})
						let newData = []
						resolve(newData)
					}
					
				})
			},
			// 加载更多事件 util.throttle为防抖函数
			loadMore: util.throttle(function(e) {
				console.log(`加载${this.currentTab}`)//currentTab表示当前所在页数 根据当前所在页数发起请求并带上page页数
				uni.showLoading({
					title: '加载中',
					mask:true
				})
				this.isRequest().then((res)=>{
					let tempList = this.listData
					if(res.length == 0){
						//停止加载
							// this.max = 1;
							this.max[this.currentTab] = 1
							console.log(this.max);
							this.$refs.scroll.setData(this.listData)
					}else{  
						tempList[this.currentTab] = tempList[this.currentTab].concat(res)
						console.log(tempList)
						this.listData = tempList
						this.$refs.scroll.setData(this.listData) //通过调用子组件的方法进行强制渲染
					}
				})
			}, 300)
			
		}
	}
</script>

<style>

</style>
